package com.deyinet.bdcms.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.deyinet.bdcms.core.vo.Page;
import com.deyinet.bdcms.entity.Syslog;
import com.deyinet.bdcms.entity.Sysmodule;
import com.deyinet.bdcms.entity.Sysrole;
import com.deyinet.bdcms.service.ILogsService;
import com.deyinet.bdcms.service.ISysrolesService;
import com.deyinet.bdcms.tools.WebUtilify;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/19 0019.
 */
@Controller
@RequestMapping(value = "/sysmanage/roles")
public class SysRolesController {

    @Autowired
    private ISysrolesService moduleService;
    @Autowired
    private ILogsService logsService;


    @RequestMapping(value = "/list")
    public void list() {
    }

    @RequestMapping(value = "/add")
    public void add(String id, Model model) {
        model.addAttribute("id", id);
    }

    @RequestMapping(value = "/listdata")
    @ResponseBody
    public Map<String, Object> listdata(final HttpServletRequest request, Page page) {
        String name = request.getParameter("name");
        Sysrole sysrole = new Sysrole();
        if (name != null && !"".equals(name)) {
            sysrole.setName(name);
        }
        moduleService.getPageByExampleLike(sysrole, page);
        Map<String, Object> info = new HashMap<>();
        info.put("totalRow", page.getTotalRow());
        info.put("pageCurrent", page.getPageCurrent());
        info.put("list", page.getList());
        return info;
    }

    @RequestMapping(value = "/roleList")
    @ResponseBody
    public List<Sysrole> roleList() {
        return moduleService.getList();
    }

    @RequestMapping(value = "/getRole")
    @ResponseBody
    public Sysrole getRole(int id) {
        return moduleService.get(id);
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> edit(@RequestBody Sysrole sysrole, HttpServletRequest request) {
        Map<String, Object> info = new HashMap<>();
        Syslog log = new Syslog();
        info.put("content", "操作失败");
        if (sysrole.getId() != null) {
            Sysrole old = moduleService.get(sysrole.getId());
            old.setName(sysrole.getName());
            old.setMemo(sysrole.getMemo());
            moduleService.updateT(old);
            log.setTitle("修改角色");
            info.put("content", "修改成功");
            log.setContent("修改id为"+old.getId()+"的角色");
        } else {
            sysrole.setCreatedate(new Timestamp(System.currentTimeMillis()));
            moduleService.insertT(sysrole);
            log.setTitle("添加角色");
            info.put("content", "添加成功");
            log.setContent("添加id为"+sysrole.getId()+"的角色");
        }
        Sysmodule sysmodule = new Sysmodule();

        sysmodule.setId(8);
        log.setSysmoduleByModuleid(sysmodule);
        log.setSysuserByUserid(WebUtilify.LoginUserinfo(request));
        log.setCreatedate(new Timestamp(System.currentTimeMillis()));
        WebUtilify.WriteLog(request,log, logsService);
        info.put("Message", "ok");
        return info;
    }

    @RequestMapping(value = "/del")
    @ResponseBody
    public Map<String, Object> del(String json, HttpServletRequest request) {
        Map<String, Object> info = new HashMap<>();
        List<Sysrole> roles = JSONArray.parseArray(json, Sysrole.class);
        Integer[] idarr = new Integer[roles.size()];
        String content="";
        for (int i = 0; i < roles.size(); i++) {
            idarr[i] = roles.get(i).getId();
            if ( idarr[i]==1){
                info.put("message", "error");
                info.put("content", "角色'管理员'不能删除");
                info.put("status", 200);
                info.put("closeCurrent", "false");
                return info;
            }

                content+=idarr[i];
            if (i!=roles.size()-1){
                content+=",";
            }
        }
        Syslog log = new Syslog();
        Sysmodule sysmodule = new Sysmodule();
        moduleService.batchDelete(idarr);
        log.setTitle("删除角色");
        sysmodule.setId(8);
        log.setSysmoduleByModuleid(sysmodule);
        log.setSysuserByUserid(WebUtilify.LoginUserinfo(request));
        log.setContent("删除id为"+content+"的角色");
        log.setCreatedate(new Timestamp(System.currentTimeMillis()));
        WebUtilify.WriteLog(request,log, logsService);

        info.put("message", "删除成功");
        info.put("status", 200);
        info.put("closeCurrent", "false");
        return info;
    }
}
